package com.js.mytransitiondemo.shareElement;

// Created by JS on 2021/7/6.

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.js.mytransitiondemo.databinding.ActivityShareElementTwoBinding;

public class ShareElementTwoActivity extends AppCompatActivity {

    private ActivityShareElementTwoBinding binding;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityShareElementTwoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        postponeEnterTransition(); // 延迟加载转场动画
        init();
    }

    private void init() {
        int url = getIntent().getIntExtra("url", 0);
        Glide.with(this)
                .load(url)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        startPostponedEnterTransition(); // 开始加载转场动画
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        startPostponedEnterTransition(); // 开始加载转场动画
                        return false;
                    }
                })
                .apply(new RequestOptions().skipMemoryCache(true))
                .into(binding.img);
    }
}
